﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyTruongHoc_App
{
    public partial class QLNhapTruong : Form
    {
        public const String OPT_DIEM_HK = "DiemHK";

        public QLNhapTruong()
        {
            InitializeComponent();
        }

        private void QLHinhThucCanBo_Load(object sender, EventArgs e)
        {
            refreshData();
        }

        private void checkUserPower()
        {
            int pow = PublicLib.getUserPower(PConst.QUYEN_QL_HINH_THUC_NHAP_TRUONG);
            // user co quyen view
            /*if (!((pow & PConst.POW_VIEW) > 0))
            {
                MessageBox.Show(MSG.MSG_KHONG_DU_QUYEN);
                this.Close();
            }*/
            // user co quyen them
            dg_DanhSach.AllowUserToAddRows =
                tsb_TaoMoi.Enabled = ((pow & PConst.POW_ADD) > 0);

            // user co quyen edit
            tsb_Edit.Enabled = ((pow & PConst.POW_EDIT) > 0);
            for (int i = 0; i < dg_DanhSach.RowCount - 1; i++)
                for (int j = 1; j < dg_DanhSach.ColumnCount; j++)
                    dg_DanhSach[j, i].ReadOnly = !((pow & PConst.POW_EDIT) > 0);

            // user co delete
            dg_DanhSach.AllowUserToDeleteRows =
                tsb_Delete.Enabled = ((pow & PConst.POW_DEL) > 0);
        }

        private void refreshData()
        {
            tbl_NhapTruongTableAdapter.Connection.ConnectionString = PublicLib.getStrConn();
            tbl_NhapTruongTableAdapter.ClearBeforeFill = true;
            this.tbl_NhapTruongTableAdapter.FillByFlag(this.qLTH.tbl_NhapTruong, PConst.FLAG_NONE);
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            dg_DanhSach.Rows[dg_DanhSach.Rows.Count - 1].Cells[grc_HinhThuc.Name].Selected = true;
            dg_DanhSach.BeginEdit(true);
        }

        private void dg_DanhSach_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dg_DanhSach.Rows[e.RowIndex].Cells[grc_id.Name].Value == null)
                return;
            if (dg_DanhSach.Rows[e.RowIndex].Cells[grc_id.Name].Value.ToString() == "") // add new
            {
                int newId = PublicLib.getMaxIdFromTbl(qLTH.tbl_NhapTruong.TableName, qLTH.tbl_NhapTruong.idColumn.ColumnName) + 1;
                dg_DanhSach.Rows[e.RowIndex].Cells[grc_id.Name].Value = newId;
            }

            dg_DanhSach.Rows[e.RowIndex].Cells[grc_Flag.Name].Value = PConst.FLAG_NONE;
        }

        private void fillSTT()
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach.Rows[i].Cells[cl_STT.Name].Value = (i + 1).ToString();
            }
        }

        private void dg_DanhSach_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            fillSTT();
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;
            dg_DanhSach.SelectedRows[0].Cells[grc_HinhThuc.Name].Selected = true;
            dg_DanhSach.BeginEdit(true);
        }

        private void dg_DanhSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            deleteByID(e.Row.Cells[grc_id.Name].Value.ToString());
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count <= 0)
                return;
            deleteByID(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
        }

        private void deleteByID(String _id)
        {
            try
            {
                DataRow[] tmp = qLTH.tbl_NhapTruong.Select(qLTH.tbl_NhapTruong.idColumn.ColumnName + "=" + _id);
                if (tmp.Length > 0)
                {
                    tmp[0][qLTH.tbl_NhapTruong.flagColumn] = PConst.FLAG_DELETE;
                    tbl_NhapTruongTableAdapter.Update(qLTH.tbl_NhapTruong);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(MSG.MSG_ERROR_INSERT_DB + " " + ex.Message);
            }
        }

        private void tblDanhGiaBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            if ((DataRowView)((BindingSource)sender).Current == null)
                return;
            DataRow ThisDataRow = ((DataRowView)((BindingSource)sender).Current).Row;

            if (ThisDataRow.RowState == DataRowState.Modified ||
                ThisDataRow.RowState == DataRowState.Added)
            {
                tbl_NhapTruongTableAdapter.Update(ThisDataRow);
            }
        }

        private void QLLoaiDiem_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Height = stt_bottom.Top - dg_DanhSach.Top - 3;
            dg_DanhSach.Width = this.Width - 5;
        }

        private void dg_DanhSach_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show(MSG.MSG_NHAP_SO);
        }

        private void QLLoaiDiem_Shown(object sender, EventArgs e)
        {
            
        }

        private void QLNhapTruong_Activated(object sender, EventArgs e)
        {
            checkUserPower();
        }

        private void tsb_In_Click(object sender, EventArgs e)
        {
            PrintDataGrid.PrintDGV print = new PrintDataGrid.PrintDGV();
            print.Print_DataGridView(dg_DanhSach);
        }
    }
}
